package io

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"strings"
)

func BufioRead_1() {
	s := strings.NewReader("ABCDEFG")
	str := strings.NewReader("12345")
	br := bufio.NewReader(s)
	// 读到F为止
	s2, _ := br.ReadString('F')
	fmt.Printf("s2: %v\n", s2)
	br.Reset(str)
	s3, _ := br.ReadString('\n')
	fmt.Printf("s3: %v\n", s3)
}

func BufioRead_2() {
	sr := strings.NewReader("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
	br := bufio.NewReader(sr)
	buf := make([]byte, 10)
	for {
		n, err := br.Read(buf)
		if err == io.EOF {
			break
		} else {
			fmt.Printf("string(buf[0:n]): %v\n", string(buf[0:n]))
		}
	}
}

func ReadByte() {
	sr := strings.NewReader("ABCDEFG")
	br := bufio.NewReader(sr)
	c, _ := br.ReadByte()
	fmt.Printf("c: %c\n", c)

	c, _ = br.ReadByte()
	fmt.Printf("c: %c\n", c)

	// 吐出一个字节
	br.UnreadByte()
	c, _ = br.ReadByte()
	fmt.Printf("c: %c\n", c)
}

func ReadRune() {
	sr := strings.NewReader("你好，世界！")
	br := bufio.NewReader(sr)

	rr, size, _ := br.ReadRune()
	fmt.Printf("rr: %c, size: %v \n", rr, size)

	br.UnreadRune()
	rr, size, _ = br.ReadRune()
	fmt.Printf("rr: %c, size: %v \n", rr, size)

}

func ReadLine() {
	sr := strings.NewReader("ABC\nDEF\r\nGHI\r\nJKL")
	br := bufio.NewReader(sr)

	line, isPrefix, _ := br.ReadLine()
	fmt.Printf("%q, %v \n", line, isPrefix)

	line, isPrefix, _ = br.ReadLine()
	fmt.Printf("%q, %v \n", line, isPrefix)

	line, isPrefix, _ = br.ReadLine()
	fmt.Printf("%q, %v \n", line, isPrefix)

	line, isPrefix, _ = br.ReadLine()
	fmt.Printf("%q, %v \n", line, isPrefix)

}

func ReadSlice() {
	sr := strings.NewReader("ABC,DEF,GHI,JKL")
	br := bufio.NewReader(sr)

	w, _ := br.ReadSlice(',')
	fmt.Printf("w: %q\n", w)

	w, _ = br.ReadSlice(',')
	fmt.Printf("w: %q\n", w)

	w, _ = br.ReadSlice(',')
	fmt.Printf("w: %q\n", w)

}

func ReadBytes() {
	sr := strings.NewReader("ABC DEF GHI JKL")
	br := bufio.NewReader(sr)

	b, _ := br.ReadBytes(' ')
	fmt.Printf("b: %q\n", b)

	b, _ = br.ReadBytes(' ')
	fmt.Printf("b: %q\n", b)

	b, _ = br.ReadBytes(' ')
	fmt.Printf("b: %q\n", b)

	b, _ = br.ReadBytes(' ')
	fmt.Printf("b: %q\n", b)

	b, _ = br.ReadBytes(' ')
	fmt.Printf("b: %q\n", b)
}

func WriteTo() {
	sr := strings.NewReader("ABCDEFGHIJKMN")
	br := bufio.NewReader(sr)
	b := bytes.NewBuffer(make([]byte, 0))

	br.WriteTo(b)
	fmt.Printf("b: %s\n", b)
}

func ResetFlush() {
	bb := bytes.NewBuffer(make([]byte, 0))
	bw := bufio.NewWriter(bb)
	bw.WriteString("1234567890")

	bb2 := bytes.NewBuffer(make([]byte, 0))

	bw.Reset(bb2)
	bw.WriteString("zhou")
	bw.Flush()

	fmt.Println(bb)
	fmt.Println(bb2)

}

func BufferedAvailable() {
	buf := bytes.NewBuffer(make([]byte, 0))
	bw := bufio.NewWriter(buf)
	fmt.Printf("bw.Available(): %v\n", bw.Available())
	fmt.Printf("bw.Buffered(): %v\n", bw.Buffered())

	bw.WriteString("ABCDEFGHIJKLM")
	fmt.Printf("bw.Available(): %v\n", bw.Available())
	fmt.Printf("bw.Buffered(): %v\n", bw.Buffered())
	fmt.Printf("buf: %q\n", buf)

	bw.Flush()
	fmt.Printf("bw.Available(): %v\n", bw.Available())
	fmt.Printf("bw.Buffered(): %v\n", bw.Buffered())
	fmt.Printf("buf: %q\n", buf)
}

func WriteByteRune() {
	buf := bytes.NewBuffer(make([]byte, 0))
	bw := bufio.NewWriter(buf)

	//写入缓存  byte 等同于 int8
	bw.WriteByte('H')
	bw.WriteByte('e')
	bw.WriteByte('l')
	bw.WriteByte('l')
	bw.WriteByte('o')
	bw.WriteByte(' ')

	// rune 等同于int32
	bw.WriteRune('世')
	bw.WriteRune('界')

	// 写入B
	bw.Flush()
	fmt.Println(buf)
}

func ReadFrom() {
	buf := bytes.NewBuffer(make([]byte, 0))
	bw := bufio.NewWriter(buf)
	sr := strings.NewReader("hello 世界！")
	bw.ReadFrom(sr)
	// ReadFrom无需使用Flush
	// bw.Flush()
	fmt.Println(buf)
}

func ReadWriter() {
	buf := bytes.NewBuffer(make([]byte, 0))
	bw := bufio.NewWriter(buf)
	s := strings.NewReader("123")
	br := bufio.NewReader(s)
	rw := bufio.NewReadWriter(br, bw)

	s2, _ := rw.ReadString('\n')
	fmt.Println(string(s2))

	rw.WriteString("zopuo")
	rw.Flush()
	fmt.Println(buf)
}

func ScanWords() {
	sr := strings.NewReader("ABC DEF GHI JKL")
	bs := bufio.NewScanner(sr)
	bs.Split(bufio.ScanWords)

	for bs.Scan() {
		fmt.Println(bs.Text())
	}

}

func ScanBytes() {
	s := strings.NewReader("Hello 世界！")
	bs := bufio.NewScanner(s)
	bs.Split(bufio.ScanBytes)
	for bs.Scan() {
		fmt.Printf("%s ", bs.Text())
	}
}

func ScanRunes() {
	s := strings.NewReader("Hello 世界！")
	bs := bufio.NewScanner(s)
	bs.Split(bufio.ScanRunes)
	for bs.Scan() {
		fmt.Printf("%s ", bs.Text())
	}
}
